﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.UserCommand;
using MediatR;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.UserCommandHandler
{
    public class AddUserCommandHandler : IRequestHandler<AddUserCommand, APIResult<object>>
    {
        private readonly IBaseRepository<User> userrReppos;
        private readonly IMapper mapper;

        public AddUserCommandHandler(IBaseRepository<User> userrReppos, IMapper mapper)
        {
            this.userrReppos = userrReppos;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(AddUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.操作成功;
            result.Msg = APIEnum.操作成功.ToString();
            var model = mapper.Map<User>(request);
            var obj = userrReppos.Add(model);
            result.data = obj;
            return Task.FromResult(result);
        }
    }
}
